package com.yuou.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yuou.annotation.AccessLimit;
import com.yuou.annotation.SystemLog;
import com.yuou.common.utils.Tree;
import com.yuou.controller.BaseExceptionController;
import com.yuou.model.BlogerRole;
import com.yuou.model.Menu;
import com.yuou.service.MenuService;
import com.yuou.util.ConstantUtil;

@Controller
@RequestMapping(value = "/admin")
public class MenuControllerAdmin extends BaseExceptionController {
	@Resource(name = "menuServiceImpl")
	private MenuService menuService;
	
	
	/**
	  * 发送消息功能
	  * @param blog
	  * @return
	  * @throws Exception
	  */
	 @RequiresPermissions("sys:menu:add")
	 @RequestMapping(value = "/addMenu",method = RequestMethod.POST)
	 @ResponseBody
	 @SystemLog(description = ConstantUtil.BLOGER_UPDATE,userType=ConstantUtil.USERTYPE_ADMIN) 
	 public Map<String, Object> addRole(String prarm,Menu menu,ServletRequest srequest) throws Exception{
		 Map<String, Object> map=new HashMap<String, Object>();
		 if(menuService.insertMenu(menu)!=0){
			 map.put("status", 200);
		 }else{
			 //0表示：更新失败
			 map.put("status", 0);
		 }
		 return map;
	 }
	 
	 
	 /**
	  * 按菜单id查询信息
	  * @param blog
	  * @return
	  * @throws Exception
	  */
	 //@RequiresPermissions("sys:menu:menu")
	 @RequestMapping(value = "/selectMenuById",method = RequestMethod.POST)
	 @ResponseBody
	 public Map<String, Object> selectMenuById(Integer id,Integer parentId) throws Exception{
		 Map<String, Object> map=new HashMap<String, Object>();
		 Menu menu=menuService.selectById(id);
		 if(parentId==0) {
			 menu.setParentName("根目录");
		 }else {
			menu.setParentName(menuService.selectNameByid(parentId));
		 }
		 if(menu!=null){
			 map.put("status", 200);
		 }
		 map.put("menu", menu);
		 return map;
	 }
	 
	 /**
	  * 更新菜单功能
	  * @param menu
	  * @return
	  * @throws Exception
	  */
	 @RequiresPermissions("sys:menu:edit")
	 @RequestMapping(value = "/updateMenu",method = RequestMethod.POST)
	 @ResponseBody
	 @SystemLog(description = ConstantUtil.BLOGER_UPDATE,userType=ConstantUtil.USERTYPE_ADMIN) 
	 public Map<String, Object> updateRole(String prarm,Menu menu) throws Exception{
		 Map<String, Object> map=new HashMap<String, Object>();
		 try {
		 if(menuService.updateByPrimaryKey(menu)!=0){
			 map.put("status", 200);
		 }else{
			 //0表示：更新失败
			 map.put("status", 0);
		 }
		 }catch(Exception e) {
			 e.printStackTrace();
		 }
		 return map;
	 }
	 
	 /**
	  * 删除消息功能
	  * @param blog
	  * @return
	  * @throws Exception
	  */
	 @RequiresPermissions("sys:menu:remove")
	 @RequestMapping(value = "/deleteMenu",method = RequestMethod.POST)
	 @ResponseBody
	 @SystemLog(description = ConstantUtil.BLOG_DELETE,userType=ConstantUtil.USERTYPE_ADMIN) 
	 public Map<String, Object> deleteMenu(Integer id) throws Exception{
		 Map<String, Object> map=new HashMap<String, Object>();
		 if(menuService.selectCountByid(id)>0) {
			 map.put("status", 202);
		 }else {
			 if(menuService.deleteByPrimaryKey(id)>0){
				 map.put("status", 200);
			 }else{
				 //0表示：删除失败
				 map.put("status", 0);
			 }
		 }
		 return map;
	 }
	 
	 /**
		 * 模糊组合分页查询角色信息(and)
		 * @param bloger
		 * @param page
		 * @param pageSize
		 * @return
		 * @throws Exception
		 */
		 //@RequiresPermissions("sys:menu:menu")
		 @RequestMapping(value = "/selectAllMenus")
		 @ResponseBody
		 @AccessLimit(seconds=1,maxCount=15)
		 /*,@RequestParam(value="pageSize", required=true,defaultValue="100") Integer pageSize*/
		 public List<Menu> selectAllblogerWithRoles(Menu menu,@RequestParam(value="page", required=true,defaultValue="1") Integer page) throws Exception{
			 Map<String, Object> map=new HashMap<String, Object>();
			 if(menu.getName()!=null&&menu.getName()!=""){
				 map.put("name", menu.getUrl());
			 }
			 if(menu.getUrl()!=null&&menu.getUrl()!=""){
				 map.put("url", menu.getUrl());
			 }
				 
			 //分页显示：第1页开始，每页显示10条记录
			 //PageHelper.startPage(page, pageSize);
			 List<Menu> menuList=menuService.selectAll(map);
			 //PageInfo<Menu> pageInfo=new PageInfo<Menu>(menuList);
			 Map<String, Object> returnMap=new HashMap<String, Object>();
			 if(menuList.size()>0){
				 returnMap.put("status", 200);
			 }else{
				 //500表示：返回值为Null
				 System.out.println("MenuController查询结果为空============");
				 returnMap.put("status", 500);
			 }
			 
			 returnMap.put("menuList", menuList);
			 //returnMap.put("pageInfo", pageInfo);
			 return menuList;
			 //return returnMap;
		 }
		 /**
			 * 模糊组合分页查询角色信息(and)
			 * @param bloger
			 */
			 //@RequiresPermissions("sys:menu:menu")
			 @RequestMapping(value = "/tree")
			 @ResponseBody
			 public Tree<Menu> tree() throws Exception{
				 Tree<Menu> tree = new Tree<Menu>();
			        tree = menuService.getTree();
			        return tree;
			 }
		 /**
			 * 模糊组合分页查询角色信息(and)
			 * @param bloger
			 */
			 //@RequiresPermissions("sys:menu:menu")
			 @RequestMapping(value = "/tree/{roleId}")
			 @ResponseBody
			 public Tree<Menu> tree(@PathVariable("roleId") Integer roleId) throws Exception{
				 Tree<Menu> tree = new Tree<Menu>();
			     tree = menuService.getTree(roleId);
			     return tree;
			 }
}
